<?php
// +----------------------------------------------------------------------
// | zlserver [ 小程序 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------
namespace app\job\import;

use think\exception\ErrorException;
use think\facade\Validate;
use think\queue\Job;
use app\common\model\User as UserModel; 
use app\common\model\BookCommon as BookCommonModel; 
use app\common\model\BookPlan as BookPlanModel; 
use app\common\model\BookInfo as BookInfoModel; 
use app\common\model\ViewGetDepts as ViewGetDeptsModel; 
use app\common\model\Dept as DeptModel;
use app\common\model\ViewGetSpecicalties as ViewGetSpecicaltiesModel; 
use app\common\model\RxndCache as RxndCacheModel; 
use app\common\model\ViewGetXsxz as ViewGetXsxzModel; 
use app\common\model\BookPlanImport as BookPlanImportModel;
use app\common\model\CwStructCache as CwStructCacheModel;
use app\common\model\Config as ConfigModel;

use app\common\model\Ietask;
use think\facade\Log;

class BookPlan
{
    protected $rule = [
        'plan_year' => 'require',
        'plan_season' => 'require',
        'grade' => 'require',
        'degree' => 'require',
        'bmmc' => 'require',
        'zymc' => 'require',
        'dir' => 'require',
        'dir' => 'require|in:就业,升学,创业', 
        'course_name' => 'require',
        'book_name' => 'require',
        'book_author' => 'require',
        'book_concern' => 'require',
        'book_isbn' => 'require|number',
        'book_edition' => 'require',
        'book_date' => 'require',
        'plan_student' => 'require|number',
        'plan_book_num' => 'require|number',
    ];
    
    protected $msg = [
        'plan_year.require' => '年度必填',
        'plan_season.require' => '季度必填',
        'grade.require' => '年级必填',
        'degree.require' => '层次必填',
        'bmmc.require' => '院系必填',
        'zymc.require' => '专业名称必填',
        'dir.in' => '方向类型错误:请参考模板性别类型介绍，或咨询系统管理员',
        'dir.require' => '方向不能为空',
        'course_name.require' => '课程名称必填',
        'book_name.require' => '教材名称必填',
        'book_author.require' => '主编必填',
        'book_concern.require' => '出版社必填',
        'book_isbn.require' => 'ISBN书号必填',
        'book_isbn.number' => 'ISBN书号非法',
        'book_edition.require' => '版次必填',
        'book_date.require' => '出版年限必填',
        'plan_student.require' => '学生数量必填',
        'plan_student.number' => '学生数量非法',
        'plan_book_num.require' => '教材数量必填',
        'plan_book_num.number' => '教材数量非法',
    ];

    // 数据校验
    public function check($fileId, $filePath)
    {
        $re = [
            'count' => 0,
            'code' => 0,
            'msg' => ''
        ];

        $bookPlanModel = new BookPlanModel();
        $viewGetDeptsModel = new ViewGetDeptsModel();
        $viewGetSpecicaltiesModel = new ViewGetSpecicaltiesModel();
        $rxndCacheModel = new RxndCacheModel();
        $viewGetXsxzModel = new ViewGetXsxzModel();
        $configModel = new ConfigModel();
        $cwStructCacheModel = new CwStructCacheModel();
        $bookInfoModel = new BookInfoModel();
        $DeptModel = new DeptModel();
        
        $data = [];
        $errorData = [];
        try {
            $resXls=import_excel($filePath);  

            if ($resXls['status']) {
                // 清理导入提示
                unset($resXls['data'][0]);
                // 取表头
                $header = $resXls['data'][1];
                unset($resXls['data'][1]);
                $title = $bookPlanModel->csvHeader();
                $fields = [];

                // 遍历表头
                $last_index = 0;
                foreach ($title as $key => $val) {
                    // 从导入的数据表中查找字段位置
                    $index = array_search($val['desc'], $header);
                    if ($index !== false) {
                        $fields[] = [
                            'index' => $index,
                            'value' => $val['id']
                        ];
                    }
                    $last_index = $index;
                }
 
                if(count($fields) != 18) { 
                    $re['msg']  = '文件格式错误！请先下载导入模板，按模板要求准备导入数据后再次导入！';
                    $re['code']  = -1;
                    return $re;
                } else {

                    // 数据填充
                    $iData = [];
                    if ($fields) {
                        $i = 0;
                        foreach ($resXls['data'] as $key => $val) {
                            foreach ($fields as $fkey => $fval) {
                                $iData[$i][$fval['value']] = $val[$fval['index']];
                                if(is_object($iData[$i][$fval['value']])) {
                                    $iData[$i][$fval['value']] = $iData[$i][$fval['value']]->__toString();
                                }
                            }
                            $i++;
                        }
                    }
    
                    // 增加描述字段
                    $fields[] = [
                        'index' => $last_index + 1,
                        'value' => 'desc'
                    ];
                    
                    $count = count($iData);
                    $errCount = 0;
     
                    // 部门
                    $deptList = $viewGetDeptsModel->getNameList();
                    // 专业
                    $SpecialtyList = $cwStructCacheModel->getSpecicaltiesList(); 
                    // 入学年度
                    $rxndList = $rxndCacheModel->getNameList();
                    // 学生性质
                    $xsxzList = $viewGetXsxzModel->getNameList();
                    // 年度，季度
                    $year = $configModel->getYear();
                    $season = $configModel->getSeason()==1?'春季':'秋季';
                    // 已存在的记录
                    $planList = $bookPlanModel->getKeyList();
                    $studentNumList = $bookPlanModel->getStudentNumList();
 
                    // 获取ISBN，名称清单
                    $isbnList = $bookInfoModel->getIsbnList();
                    $bookInfoList = $bookInfoModel->getBookInfoList();

                    //获取部门管理名称列表
                    $deptNameList = $DeptModel->getDeptList();


                    $tmp_arr = array(); 
                    $tmp_book_arr = array(); 
                    $tmp_num_arr = array(); 
                    
                    $exceptInfo = ['作业本', '英语本', '实验报告册', '课程设计用纸'];
                    // 数据预处理 
                    foreach ($iData as $key => $val) {
                        $errFind = false;
                        //校验数据
                        $validate = new \think\Validate($this->rule, $this->msg);
                        $validate->batch(true);

                        // 数据预处理
                        $time = time();
                        $bookplan['plan_year'] = trim($val['plan_year']);
                        $bookplan['plan_season'] = trim($val['plan_season']);
                        $bookplan['grade'] = trim($val['grade']);
                        $bookplan['degree'] = trim($val['degree']);
                        $bookplan['bmmc'] = trim($val['bmmc']);
                        $bookplan['zymc'] = trim($val['zymc']);
                        $bookplan['dir'] = trim($val['dir']);
                        $bookplan['course_name'] = trim($val['course_name']);
                        $bookplan['book_name'] = trim($val['book_name']);
                        $bookplan['book_author'] = trim($val['book_author']);
                        $bookplan['book_concern'] = trim($val['book_concern']);
                        $bookplan['book_isbn'] = trim($val['book_isbn']);
                        $bookplan['book_edition'] = trim($val['book_edition']);
                        $bookplan['book_date'] = trim($val['book_date']);
                        $bookplan['plan_student'] = trim($val['plan_student']);
                        $bookplan['plan_book_num'] = trim($val['plan_book_num']);
                        $bookplan['book_reward'] = trim($val['book_reward']);
                        $bookplan['description'] = trim($val['description']);
                        
                        $bookplan['desc'] = '';
                        $bookplan['error'] = 0;
 
                        if (!$validate->check($bookplan)) {
                            $bookplan['error'] = 1;
                            $errCount = $errCount + 1;
                            $errFind = true;

                            $index = 0;
                            foreach ($validate->getError() as $errKey => $value) {
                                if($index == 0) {
                                    $bookplan['desc']  = $bookplan['desc'] . $value;
                                } else {
                                    $bookplan['desc']  = $bookplan['desc'] . ';' . $value;
                                }
                                $index++;
                            }  
                        }

                        // 年度
                        if($bookplan['plan_year'] != $year){
                            $bookplan['error'] = 1;
                            if(strpos($bookplan['desc'], '年度') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['plan_year'] . '非当前年度';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $bookplan['plan_year'] . '非当前年度';
                                }
                            }
                        }

                        // 季度
                        if($bookplan['plan_season'] != $season){
                            $bookplan['error'] = 1;
                            if(strpos($bookplan['desc'], '季度') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['plan_season'] . '非当前季度';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $bookplan['plan_season'] . '非当前季度';
                                }
                            }
                        }

                        // 判断院系是否存在
                        if (!in_array($bookplan['bmmc'], $deptList)) {
                            $bookplan['error'] = 1;

                            if(strpos($bookplan['desc'], '院系') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['bmmc'] . '院系不存在';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $bookplan['bmmc'] . '院系不存在';
                                }
                            }
                        }

                        // 判断部院在部门管理中是否存在
                        if (!in_array($bookplan['bmmc'], $deptNameList)) {
                            $bookplan['error'] = 1;

                            if(strpos($bookplan['desc'], '部院') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['bmmc'] . '在部门管理中不存在，请联系管理员增加';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $bookplan['bmmc'] . '在部门管理中不存在，请联系管理员增加';
                                }
                            }
                        }

                        $keyzymc = $bookplan['bmmc'].'-'.$bookplan['zymc'].'-'.$bookplan['grade'].'-'.$bookplan['degree'];
                        // 判断专业是否存在
                        if (!in_array($keyzymc, $SpecialtyList)) {
                            $bookplan['error'] = 1;

                            if(strpos($bookplan['desc'], '专业') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $keyzymc . '不存在';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $keyzymc . '不存在';
                                }
                            }
                        }
                        
                        // 判断年级是否存在
                        if (!in_array($bookplan['grade'], $rxndList)) {
                            $bookplan['error'] = 1;

                            if(strpos($bookplan['desc'], '年级') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['grade'] . '年级不存在';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $bookplan['grade'] . '年级不存在';
                                }
                            }
                        }
                        
                        // 判断层次是否存在
                        if (!in_array($bookplan['degree'], $xsxzList)) {
                            $bookplan['error'] = 1;

                            if(strpos($bookplan['desc'], '层次') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['degree'] . '层次不存在';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $bookplan['degree'] . '层次不存在';
                                }
                            }
                        }
 
                        if (!in_array($bookplan['degree'], $xsxzList)) {
                            $bookplan['error'] = 1;

                            if(strpos($bookplan['desc'], '层次') === false) { 
                                if($errFind) {
                                    $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['degree'] . '层次不存在';
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] =  $bookplan['degree'] . '层次不存在';
                                }
                            }
                        }

                        $keydbExist = $bookplan['bmmc'].'-'.$bookplan['zymc'].'-'.$bookplan['grade'].'-'.$bookplan['degree'].'-'.$bookplan['dir'].'-'.$bookplan['plan_year'].'-'.$bookplan['plan_season'].'-'.$bookplan['book_isbn'];
                        // 数据库检测 
                        if (in_array($keydbExist, $planList)) {
                            $bookplan['error'] = 1;
                            if($errFind) {
                                $bookplan['desc'] = $bookplan['desc'] . ';' . '计划已存在：'.$keydbExist; 
                            } else {
                                $errCount = $errCount + 1;
                                $errFind = true;
                                $bookplan['desc'] = $bookplan['desc'] . '计划已存在：'.$keydbExist; 
                            }
                        }

                        $keydbnumExist = $bookplan['bmmc'].'-'.$bookplan['zymc'].'-'.$bookplan['grade'].'-'.$bookplan['degree'].'-'.$bookplan['dir'].'-'.$bookplan['plan_year'].'-'.$bookplan['plan_season'];
                        
                        if(array_key_exists($keydbnumExist, $studentNumList)) {
                            $keydbNum =  $studentNumList[$keydbnumExist];
                            
                            // 数量检测 
                            if ($keydbNum != $bookplan['plan_student']) {
                                $bookplan['error'] = 1;
                                if($errFind) {
                                    $bookplan['desc'] = $bookplan['desc'] . ';' . '学生数量不一致'; 
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] = $bookplan['desc'] . '学生数量不一致'; 
                                }
                            }
                        } else {
                            // 自身检测 
                            if (array_key_exists($keydbnumExist, $tmp_num_arr)) { 
                                $keydbNum =  $tmp_num_arr[$keydbnumExist];
                                if ($keydbNum != $bookplan['plan_student']) {
                                    $bookplan['error'] = 1;
                                    if($errFind) {
                                        $bookplan['desc'] = $bookplan['desc'] . ';' . '导入数据中学生数量不一致'; 
                                    } else {
                                        $errCount = $errCount + 1;
                                        $errFind = true;
                                        $bookplan['desc'] = $bookplan['desc'] . '导入数据中学生数量不一致'; 
                                    }
                                }
                            } else {
                                if($keydbnumExist != '') {
                                    $tmp_num_arr[$keydbnumExist] = $bookplan['plan_student'];
                                }
                            }
                        }

                        //判断教材数量是否为0
                        if ($bookplan['plan_book_num'] == 0) {
                            $bookplan['error'] = 1;
                                if($errFind) {
                                    $bookplan['desc'] = $bookplan['desc'] . ';' . '教材数量不能为零，请输入正确的教材数量！'; 
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] = $bookplan['desc'] . '教材数量不能为零，请输入正确的教材数量！'; 
                                }
                        }
                        
                        //判断ISBN号是否为13位有效数字
                        if (!preg_match("/^\d{13}$/", $bookplan['book_isbn'])) {
                            $bookplan['error'] = 1;
                                if($errFind) {
                                    $bookplan['desc'] = $bookplan['desc'] . ';' . 'ISBN书号必须为13位的纯数字，请规范填写！'; 
                                } else {
                                    $errCount = $errCount + 1;
                                    $errFind = true;
                                    $bookplan['desc'] = $bookplan['desc'] . 'ISBN书号必须为13位的纯数字，请规范填写！'; 
                                }
                        }

                        // 判断ISBN是否存在
                        //if (!in_array($bookplan['book_isbn'], $isbnList)) {
                        //    $bookplan['error'] = 1;
//
                        //    if(strpos($bookplan['desc'], 'ISBN') === false) { 
                        //        if($errFind) {
                        //            $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['book_isbn'] . 'ISBN不存在';
                        //        } else {
                        //            $errCount = $errCount + 1;
                        //            $errFind = true;
                        //            $bookplan['desc'] =  $bookplan['book_isbn'] . 'ISBN不存在';
                        //        }
                        //    }
                        //} else  {
						if ($bookplan['book_isbn'] != '') {
                            if (in_array($bookplan['book_isbn'], $isbnList)) {
                                // 判断图书是否正确
                                if (!array_key_exists($bookplan['book_isbn'].'-'.$bookplan['book_name'], $bookInfoList)) {
                                    $bookplan['error'] = 1; 
                                    if($errFind) {
                                        $bookplan['desc'] =  $bookplan['desc'] . ';' . $bookplan['book_name'] . '与' . $bookplan['book_isbn'] . '不匹配';
                                    } else {
                                        $errCount = $errCount + 1;
                                        $errFind = true;
                                        $bookplan['desc'] =  $bookplan['book_name'] . '与' . $bookplan['book_isbn'] . '不匹配';
                                    } 
                                }
                            }
                        }
                        // 自身检测 图书
                        if (array_key_exists($bookplan['book_isbn'], $tmp_book_arr)) { 
                            if($bookplan['book_name'] != $tmp_book_arr[$bookplan['book_isbn']]) {
                                if(strpos($bookplan['desc'], '不匹配') === false) { 
                                    $bookplan['error'] = 1; 
                                    if($errFind) {
                                        $bookplan['desc'] =  $bookplan['desc'] . ';' . '导入数据中' . $bookplan['book_name'] . '与' . $bookplan['book_isbn'] . '不匹配';
                                    } else {
                                        $errCount = $errCount + 1;
                                        $errFind = true;
                                        $bookplan['desc'] =  '导入数据中' . $bookplan['book_name'] . '与' . $bookplan['book_isbn'] . '不匹配';
                                    } 
                                }
                            } 
                        } else {
                            $tmp_book_arr[$bookplan['book_isbn']] = $bookplan['book_name'];
                        }

                        $keyExist = $bookplan['bmmc'].'-'.$bookplan['zymc'].'-'.$bookplan['grade'].'-'.$bookplan['degree'].'-'.$bookplan['dir'].'-'.$bookplan['plan_year'].'-'.$bookplan['plan_season'].'-'.$bookplan['book_isbn'].'-'.$bookplan['book_edition'].'-'.$bookplan['book_date'];
                        // 自身检测 
                        if (in_array($keyExist, $tmp_arr) && !in_array($bookplan['book_name'], $exceptInfo)) { //搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                            $bookplan['error'] = 1;
                            if($errFind) {
                                $bookplan['desc'] = $bookplan['desc'] . ';' . '导入数据中存在重复数据：'.$keyExist; 
                            } else {
                                $errCount = $errCount + 1;
                                $errFind = true;
                                $bookplan['desc'] = $bookplan['desc'] . '导入数据中存在重复数据：'.$keyExist; 
                            }
                        } else {
                            if($keyExist != '') {
                                $tmp_arr[] = $keyExist;
                            }
                        }

                        if ($bookplan['error'] == 1) {
                            $errorData[$errCount - 1] = $bookplan;
                        }
    
                        $data[$key] = $bookplan;
                        unset($bookplan);
                    }
                } 
            } else {
                $re['msg']  = $resXls['msg'];
                $re['code']  = -1;
                return $re;
            }
        } catch (Exception $e) {
            $re['msg']  = $e->getMessage();
            $re['code']  = -1;
            return $re;
        }

        $re['count'] = $count;
        $re['errCount'] = $errCount;
        $re['data'] = json_encode($data);
        $re['fileId'] = $fileId;
        $re['filePath'] = $filePath;
        $re['errData'] = json_encode($errorData);
        return $re;
    }

    //执行导入任务
    public function exec(Job $job, $params)
    {
        $ietaskModle = new Ietask();

        $ietaskInfo = $ietaskModle->where(['id' => $params['task_id']])->find();

        $bookPlanModel = new BookPlanModel();
        $bookPlanImportModel = new BookPlanImportModel();
        $message = [];
        $count = 0;         // 记录总数
        $progress = 0;
        try {
            $file = json_decode($params['params'], true);
            $resXls=import_excel($file['file_path']); 
            $count = count($resXls['data'])-2;

            if ($resXls['status']) {
                // 清理导入提示
                unset($resXls['data'][0]);
                // 取表头
                $header = $resXls['data'][1];
                unset($resXls['data'][1]);
                $title = $bookPlanModel->csvHeader(); 
                 
                $fields = []; 
                foreach ($title as $key => $val) {
                    $index = array_search($val['desc'], $header);
                    if ($index >= 0) {
                        $fields[] = [
                            'index' => $index,
                            'value' => $val['id']
                        ];
                    }
                }

                // 提取所有教材计划信息
                $iData = [];
                if ($fields) {
                    $i = 0;
                    foreach ($resXls['data'] as $key => $val) {
                        foreach ($fields as $fkey => $fval) {
                            $iData[$i][$fval['value']] = $val[$fval['index']];
                            if(is_object($iData[$i][$fval['value']])) {
                                $iData[$i][$fval['value']] = $iData[$i][$fval['value']]->__toString();
                            }
                        }
                        $i++;
                    }
                }
  
                // 清空临时表
                $bookPlanImportModel->where(['manage_id' => $ietaskInfo['manage_id']])->delete();

                // 学生信息写入临时表
                unset($bookplan_comment);
                $bookplan_comment = []; 
                foreach ($iData as $key => $val) {
                    $bookplan_comment[] = [
                        'plan_year'         => trim($val['plan_year']),
                        'plan_season'       => trim($val['plan_season']),
                        'grade'             => trim($val['grade']),
                        'degree'            => trim($val['degree']),
                        'bmmc'              => trim($val['bmmc']),
                        'zymc'              => trim($val['zymc']),
                        'dir'               => trim($val['dir']),
                        'course_name'       => trim($val['course_name']),
                        'book_name'         => trim($val['book_name']),
                        'book_author'       => trim($val['book_author']),
                        'book_concern'      => trim($val['book_concern']),
                        'book_isbn'         => trim($val['book_isbn']),
                        'book_edition'      => trim($val['book_edition']),
                        'book_date'         => trim($val['book_date']),
                        'plan_student'      => trim($val['plan_student']),
                        'plan_book_num'     => trim($val['plan_book_num']),
                        'book_reward'       => trim($val['book_reward']),
                        'manage_id'         => trim($ietaskInfo['manage_id']),
                    ];
     
                    // 每隔100条数据更新一次数据处理进度
                    $progress++; 
                    if($progress %100==0) {
                        // 批量插入数据
                        $bookPlanImportModel->startTrans(); 
                        $num = $bookPlanImportModel->insertAll($bookplan_comment);
                        if($num != 100) {
                            $bookPlanImportModel->rollback();
                            Log::record($bookplan_comment . '用户数据保存失败');
                        } else {                        
                            $bookPlanImportModel->commit();
                        }

                        // 清理环境
                        unset($bookplan_comment);
                        $bookplan_comment = [];
                    }
                }

                // 批量插入数据
                $bookPlanImportModel->startTrans(); 
                $num = $bookPlanImportModel->insertAll($bookplan_comment);               
                $bookPlanImportModel->commit(); 

                // 执行存储过程批量更新学生信息
                $bookPlanImportModel->query("call programBookPlan('".$ietaskInfo['manage_id']."');");
        
                // 清空临时表
                //$bookPlanImportModel->where(['manage_id' => $ietaskInfo['manage_id']])->delete();

                $uData['status'] = $ietaskModle::IMPORT_SUCCESS_STATUS;
                $uData['message'] = '导入成功';
                if ($message) {
                    $uData['message'] .= json_encode($message);
                }

                $uData['count'] = $count;
                $ietaskModle->update($uData, ['id' => $params['task_id']]);
            } else {
                $uData['status'] = $ietaskModle::IMPORT_FAIL_STATUS;
                $uData['message'] = $resXls['msg'];
                $uData['utime'] = time();
                $ietaskModle->update($uData, ['id' => $params['task_id']]);
            }
        } catch (Exception $e) {
            $message[] = $e->getMessage();
        }
        if ($job->attempts() > 3) {
            $uData['status'] = $ietaskModle::IMPORT_FAIL_STATUS;
            $uData['message'] = '导入执行失败';
            $uData['utime'] = time();
            $ietaskModle->update($uData, ['id' => $params['task_id']]);
            $job->delete();
        }
    }

    public function failed($data)
    {

        // ...任务达到最大重试次数后，失败了
    }
}
